<template>
  <div class="merchat-list">
    <Breadcrumb>
      <BreadcrumbItem v-for="(name, i) in breadcrumb" :key="i">{{ name }}</BreadcrumbItem>
    </Breadcrumb>
    <div class="list-title">查询条件</div>
    <Row type="flex">
      <Form ref="formInline" :model="formInline" inline :label-width="0" label-position="left">
        <FormItem label="" prop="searchContent">
          <Input type="text" v-model="formInline.searchContent" placeholder="人员姓名、手机号" clearable style="width: 300px;"/>
        </FormItem>
        <FormItem prop="status" label="">
          <Select v-model="formInline.apiType" placeholder="请求类型" style="minWidth:162px" filterable clearable>
            <Option v-for="item in apiTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </FormItem>
        <FormItem prop="date" label="">
          <DatePicker ref="datePicker" type="daterange" @on-change="selectDate" placeholder="请选择时间" style="width: 200px;"></DatePicker>
        </FormItem>
        <div class="form-but">
          <Button type="success" @click="getList">查询</Button>
          &nbsp;&nbsp;
          <Button @click="handleReset('formInline')">清空</Button>
        </div>
      </Form>
    </Row>
    <div>
      <Table class="list-table"
        stripe
        border
        ref="selection"
        :columns="columns"
        :data="dataList"
      ></Table>
      <div class="table-page">
        <Page :total="total" show-sizer show-elevator show-total @on-change="changePageNum" @on-page-size-change="changePageSize" />
        <ApiLogDetailModal ref="apiLogDetail" v-bind:apiLogInfo="apiLog" />
      </div>
    </div>
  </div>
</template>

<script>
import { formatTime } from '@/utils'
import Api from '@/api'
import ApiLogDetailModal from './apiLogDetailModal.vue'

const systemApiLogApi = Api.SystemApiLog

export default {
  name: 'login',
  components: {
    ApiLogDetailModal
  },
  data() {
    return {
      breadcrumb: ['系统管理', '日志管理'],
      formInline: {
        apiType: '',
        searchContent: '',
        createTimeStart: '',
        createTimeEnd: '',
      },
      apiLog: {

      },
      apiLogList: [],
      apiTypeList: [
        {
          label: '渠道请求V10',
          value: '1'
        },
        {
          label: 'V10请求渠道',
          value: '2'
        }
      ],
      dataList: [],
      columns: [
        {
          title: '主键Id',
          key: 'id',
          minWidth: 120
        },
        {
          title: '创建时间',
          key: 'createTime',
          minWidth: 200,
          render: (h, params) => (
            h('div', {}, formatTime(params.row.createTime, 'second'))
          )
        },
        {
          title: '请求地址',
          key: 'url',
          minWidth: 180
        },
        {
          title: '请求内容',
          key: 'requestContent',
          minWidth: 180,
          tooltip: true,
          render: (h, params) => (
            h('Input', {
              props: {
                value: params.row.requestContent
              },
            }, params.row.requestContent)
          )
        },
        {
          title: 'V10报备Uuid',
          key: 'intentionUuid',
          minWidth: 250,
          tooltip: true
        },
        {
          title: '返回内容',
          key: 'responseContent',
          minWidth: 180,
          render: (h, params) => (
            h('Input', {
              props: {
                value: params.row.responseContent
              },
            }, params.row.responseContent)
          )
        },
        {
          title: '返回结果',
          key: 'status',
          minWidth: 180
        },
        {
          title: '业务Id',
          key: 'otherRelationId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '业务类型',
          key: 'businessType',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '渠道公司',
          key: 'companyId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: 'V10公司',
          key: 'otherCompanyId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '楼盘Id',
          key: 'premisesId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '楼盘名称',
          key: 'premisesName',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '经纪人Id',
          key: 'partnerId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '经纪人姓名',
          key: 'partnerName',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '经纪人公司Id',
          key: 'agentCompanyId',
          minWidth: 180,
          tooltip: true,
        },
        {
          title: '操作',
          key: 'operate',
          minWidth: 80,
          fixed: 'right',
          render: (h, params) => (
            h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    this.showReportDetail(params.row);
                  }
                }
              }, '查看'),
            ])
          )
        }
      ],
      page: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      ids: []
    }
  },
  methods: {
    selectDate(date) {
      this.formInline.createTimeStart = date[0]
      this.formInline.createTimeEnd = date[1]
    },
    // 分页变化
    changePageNum(pageNum) {
      this.getList({ pageNum })
    },
    // 条数变化
    changePageSize(pageSize) {
      this.getList({ pageSize })
    },
    async handleReset(name) {
      this.$refs[name].resetFields()
      this.$refs.datePicker.handleClear()
      this.formInline.createTimeStart = ''
      this.formInline.createTimeEnd = ''
      this.page = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    async getList(paramsPage = null) {
      let pageNum
      let pageSize
      let page
      const statePage = this.page
      if (paramsPage) {
        pageNum = paramsPage.pageNum || statePage.pageNum
        pageSize = paramsPage.pageSize || statePage.pageSize
        page = { pageNum, pageSize }
        this.page = { pageNum, pageSize }
      } else {
        page = statePage
      }
      const formInline = this.formInline
      const value = {}
      Object.keys(formInline).forEach(key => {
        if (formInline[key]) {
          value[key] = formInline[key]
        }
      })
      const params = {
        page,
        ...value
      }
      const { responseCode, data } = await this.$request(systemApiLogApi.apiLogList, params)
      if (responseCode === '1') {
        this.dataList = data.list
        this.total = data.totalCount
      }
    },
    showReportDetail(params) {
      this.apiLog = params;
      this.$refs.apiLogDetail.cleanParams();
      this.$refs.apiLogDetail.initData(params);
    }
  },
  mounted() {
    this.getList()
  },
}
</script>
<style lang="scss" scoped>
.merchat-list {
  .ivu-form-item {
    margin-right: 20px;
  }
  .form-but {
    display: inline-block;
    margin-bottom: 24px;
  }
  .list-title {
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    color: #333;
  }
  .add {
    margin-bottom: 10px;
  }
  .table-page {
    display: flex;
    justify-content: space-between;
    margin-top: 15px;
  }
  .ivu-table .table-success-row td{
    background-color: greenyellow;
    color: #fff;
  }
  .ivu-table .table-fail-row td{
    background-color: red;
    color: #fff;
  }
}
</style>
